package keye.document_manage.util;

import java.util.HashMap;
import java.util.List;

import keye.document_manage.entity.TableFiled;

public class SqliteDialect implements Dialect{

	@Override
	public String getPageSql(String sql, long offset, long limit) {
		return sql + " limit "+offset+","+limit;
	}

	@Override
	public String changeType(String type) {
		type = type.trim().toUpperCase();
		String newType = "";
		switch (type) {
		case "INT":
		case "INTEGER":
		case "TINYINT":
		case "SMALLINT":
		case "MEDIUMINT":
		case "BIGINT":
		case "UNSIGNED BIG INT":
		case "INT2":
		case "INT8":	newType = "INTEGER";break;
		case "CHARACTER":
		case "VARCHAR":
		case "VARYING CHARACTER":
		case "NCHAR":
		case "NATIVE CHARACTER":
		case "NVARCHAR":
		case "TEXT":
		case "CLOB":
		case "DATE":
		case "DATETIME":newType="TEXT";break;
		case "REAL":
		case "DOUBLE":
		case "DOUBLE PRECISION":
		case "FLOAT":	newType="REAL";break;
		case "NUMERIC":
		case "DECIMAL":
		case "BOOLEAN":	newType="NUMERIC";break;

		default:	newType="NONE";
			break;
		}
		return newType;
	}

	@Override
	public String getInsertSql(List<TableFiled> fileds,
			List<HashMap<String, Object>> values) {
		StringBuilder sb = new StringBuilder();
		values.forEach(m->{
			sb.append("(");
			fileds.forEach(f->{
				Object o = m.get(f.getName());
				if(f.getType().equals("TEXT")&& o!=null)
					sb.append("'"+o+"' ,");
				else
					sb.append(o+",");
			});
			sb.deleteCharAt(sb.length()-1);
			sb.append("),");
		});
		sb.deleteCharAt(sb.length()-1);
		return sb.toString();
	}
}
